#include <bits/stdc++.h>
using namespace std;
const int maxn = 30+2;
typedef long long ll;
ll n, d[maxn][maxn], w[maxn], m[maxn][maxn];

ll D(int i, int j){
    if(i>j) return 1;
    if(i==j) return w[i];
    ll& ans = d[i][j];
    if(ans) return ans;
    for (int k = i; k <= j; ++k) {
        int v = D(i, k-1) * D(k+1, j) + w[k];
        if(ans < v) ans = v, m[i][j] = k;
    }
    return ans;
}
bool fir = true;
void print(int i, int j){
    if(i>j) return;
    if(fir) fir = false;
    else cout << " ";
    if(i == j) cout << i;
    else{
        int k = m[i][j];
        cout << k;
        print(i, k-1); print(k+1, j);
    }
}
int main() {
    ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> w[i];
    }
    cout << D(1, n) << endl;
    print(1, n);
    return 0;
}